package com.wl.application.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wl.application.common.BusinessException;
import com.wl.application.common.Result;
import com.wl.application.entity.Emp;
import com.wl.application.services.IEmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/15
 * @description:
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private IEmpService empService;


    @PostMapping("/page")
    public Result page(@RequestBody Page<Emp> page){
        return Result.ok(empService.page(page));
    }

    @GetMapping("/list")
    public Result list(){
        return Result.ok(empService.list());
    }
    @PostMapping("/save")
    public Result save(@RequestBody Emp emp){
        return empService.save(emp)? Result.ok():Result.error();
    }

    @DeleteMapping("del/{empno}")
    public Result del(@PathVariable("empno") Integer empno){
        return empService.removeById(empno)?Result.ok():Result.error();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Emp emp){
        return Result.write(empService.updateById(emp));
    }
    @GetMapping("/getEmp")
    public Result getEmp(Integer eno){
        System.out.println(eno);
        if (eno==1) {
            //出现空指针异常
            System.out.println(10 / 0);
        }else{
            throw  new BusinessException();
        }
        return Result.ok(empService.getById(eno));
    }

}
